﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chemicall
{
    public class SoundManager
    {
        private static SoundManager instance;

        //FMOD.System system;
        Dictionary<int, FMOD.Event> soundList;
        //FMOD.Channel FXchannel;

        FMOD.EventSystem eventsystem = null;
        FMOD.RESULT result;

        public static SoundManager getInstance()
        {
            if (instance == null)
                instance = new SoundManager();
            return instance;
        }

        private SoundManager()
        {
            /*FMOD.Factory.System_Create(ref system);
            system.init(1, FMOD.INITFLAGS.NORMAL, IntPtr.Zero);
            FXchannel = new FMOD.Channel();
            FXchannel.setVolume(0.5f);*/

            result = FMOD.Event_Factory.EventSystem_Create(ref eventsystem);
            result = eventsystem.init(64, FMOD.INITFLAGS.NORMAL, IntPtr.Zero, FMOD.EVENT_INITFLAGS.NORMAL);
            result = eventsystem.setMediaPath("../../../../../soundProject/");
            eventsystem.load("chemicallSP.fev");
            soundList = new Dictionary<int, FMOD.Event>();

        }

        public void initEvent(int id, string path)
        {

            FMOD.Event evt = null;
            result = eventsystem.getEvent(path, FMOD.EVENT_MODE.DEFAULT, ref evt);
            if (result != FMOD.RESULT.OK)
            {
                Console.Write("error");
            }
            if (!soundList.ContainsKey(id))
                soundList.Add(id, evt);

        }

        public void startEvent(int soundId)
        {
            if (soundList.ContainsKey(soundId))
                soundList[soundId].start();
        }

        public void stopEvent(int soundId)
        {
            if (soundList.ContainsKey(soundId))
            {
                soundList[soundId].stop();
            }
        }

        public void setParameterValue(int soundId, string paramName, float value)
        {
            if (soundList.ContainsKey(soundId))
            {
                FMOD.EventParameter param = null;
                result = soundList[soundId].getParameter(paramName, ref param);
                param.setValue(value);
            }
        }
    }
}
